package com.xunyin.modules.coupon.rest;

import com.github.pagehelper.util.StringUtil;
import com.xunyin.annotation.AnonymousAccess;
import com.xunyin.common.dto.ParamDto;
import com.xunyin.constant.Constants;
import com.xunyin.enums.CouponTypeEnum;
import com.xunyin.enums.CouponUseForEnum;
import com.xunyin.enums.StatusEnum;
import com.xunyin.enums.WeekDayEnum;
import com.xunyin.modules.couponnew.domain.MtUserCoupon;
import com.xunyin.modules.couponnew.service.UserCouponService;
import com.xunyin.modules.couponnew.service.mapper.MtUserCouponMapper;
import com.xunyin.modules.security.security.TokenUtil;
import com.xunyin.modules.security.security.vo.JwtUser;
import com.xunyin.pagination.PaginationRequest;
import com.xunyin.pagination.PaginationResponse;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 卡券管理类controller
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Slf4j
@AllArgsConstructor
@Api(tags = "共享空间:用户卡券管理类")
@RestController
@RequestMapping("api/userCoupon")
public class BackendUserCouponController {

    private final UserCouponService userCouponService;

    private final MtUserCouponMapper mtUserCouponMapper;
    private final TokenUtil tokenUtil;

    /**
     * 查询卡券列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity<Map<String, Object>> list(HttpServletRequest request) {

        Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
        Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
/*        Integer groupId = (request.getParameter("groupId") == null || StringUtil.isEmpty(request.getParameter("groupId"))) ? 0 : Integer.parseInt(request.getParameter("groupId"));
        Integer couponId = (request.getParameter("id") == null || StringUtil.isEmpty(request.getParameter("id"))) ? 0 : Integer.parseInt(request.getParameter("id"));*/
        String name = request.getParameter("name") == null ? "" : request.getParameter("name");
        String mobile = request.getParameter("mobile") == null ? "" : request.getParameter("mobile");
        String type = request.getParameter("type") == null ? "" : request.getParameter("type");
        String status = request.getParameter("status") == null ? "" : request.getParameter("status");
        Integer storeId = (request.getParameter("storeId") == null || StringUtil.isEmpty(request.getParameter("storeId"))) ? 0 : Integer.parseInt(request.getParameter("storeId"));

        PaginationRequest paginationRequest = new PaginationRequest();
        paginationRequest.setCurrentPage(page);
        paginationRequest.setPageSize(pageSize);

        Map<String, Object> params = new HashMap<>();

/*        if (couponId > 0) {
            params.put("id", couponId.toString());
        }*/
        if (StringUtil.isNotEmpty(name)) {
            params.put("name", name);
        }
        if (StringUtil.isNotEmpty(mobile)) {
            params.put("mobile", mobile);
        }
        if (StringUtil.isNotEmpty(type)) {
            params.put("type", type);
        }
        if (StringUtil.isNotEmpty(status)) {
            params.put("status", status);
        }
//        if (storeId > 0) {
//            searchParams.put("storeId", storeId);
//        }
        JwtUser user = tokenUtil.getJwtUser(tokenUtil.getToken(request));
        if (!user.getRoleIdList().contains(Long.valueOf(1L))) {
            params.put("storeIdList", user.getStoreIdList());
        }

        paginationRequest.setSearchParams(params);
        paginationRequest.setSortColumn(new String[]{"status asc", "createTime desc"});
        PaginationResponse<MtUserCoupon> paginationResponse = userCouponService.queryUserCouponListByPagination(paginationRequest);
        //List<MtUserCoupon> dataList = paginationResponse.getContent();

        Map<String, Object> paramsStore = new HashMap<>();
        paramsStore.put("status", StatusEnum.ENABLED.getKey());
        if (storeId > 0) {
            paramsStore.put("storeId", storeId);
        }

        // 卡券类型列表
        CouponTypeEnum[] typeListEnum = CouponTypeEnum.values();
        List<ParamDto> typeList = new ArrayList<>();
        for (CouponTypeEnum enumItem : typeListEnum) {
            if (enumItem.getKey().equals(CouponTypeEnum.HOUR.getKey())
                    || enumItem.getKey().equals(CouponTypeEnum.NUMTIMES.getKey())
                    || enumItem.getKey().equals(CouponTypeEnum.COUPON.getKey())) {

                ParamDto paramDto = new ParamDto();
                paramDto.setKey(enumItem.getKey());
                paramDto.setName(enumItem.getValue());
                paramDto.setValue(enumItem.getKey());
                typeList.add(paramDto);
            }
        }

        // 状态列表
        StatusEnum[] statusListEnum = StatusEnum.values();
        List<ParamDto> statusList = new ArrayList<>();
        for (StatusEnum enumItem : statusListEnum) {
            ParamDto paramDto = new ParamDto();
            paramDto.setKey(enumItem.getKey());
            paramDto.setName(enumItem.getValue());
            paramDto.setValue(enumItem.getKey());
            statusList.add(paramDto);
        }

        // 卡券使用专项列表
        CouponUseForEnum[] couponUseForEnum = CouponUseForEnum.values();
        List<ParamDto> couponUseForList = new ArrayList<>();
        for (CouponUseForEnum enumItem : couponUseForEnum) {
            ParamDto paramDto = new ParamDto();
            paramDto.setKey(enumItem.getKey());
            paramDto.setName(enumItem.getValue());
            paramDto.setValue(enumItem.getKey());
            couponUseForList.add(paramDto);
        }

        // 星期列表
        WeekDayEnum[] weekDayEnum = WeekDayEnum.values();
        List<ParamDto> weekDayForList = new ArrayList<>();
        for (WeekDayEnum enumItem : weekDayEnum) {
            ParamDto paramDto = new ParamDto();
            paramDto.setKey(enumItem.getKey());
            paramDto.setName(enumItem.getValue());
            paramDto.setValue(enumItem.getKey());
            weekDayForList.add(paramDto);
        }

        // 会员等级列表
        Map<String, Object> param = new HashMap<>();
        param.put("status", StatusEnum.ENABLED.getKey());
        Map<String, Object> result = new HashMap<>();
        //result.put("storeList", storeList);
        result.put("typeList", typeList);
        result.put("statusList", statusList);
        result.put("couponUseForList", couponUseForList);
        result.put("weekDayForList", weekDayForList);
        result.put("paginationResponse", paginationResponse);
        return new ResponseEntity<>(result, HttpStatus.OK);
        //return getSuccessResult(result);
    }

    /**
     * 获取用户卡券统计数据
     * @return ConponStatDto
     */
    @GetMapping(value = "/data/getUserConponStat")
    @AnonymousAccess
    public ResponseEntity getUserConponStat(HttpServletRequest request,
                                            @RequestParam("storeId") Integer storeId,
                                            @RequestParam("type") String type,
                                            @RequestParam("status") String status,
                                            @RequestParam("mobile") String mobile) {
        JwtUser user = tokenUtil.getJwtUser(tokenUtil.getToken(request));
        String storeIds = "-1";
        if (!user.getRoleIdList().contains(Long.valueOf(1L))) {
            storeIds = StringUtils.join(user.getStoreIdList(), ",");
        }
        return new ResponseEntity<>(userCouponService.getUserConponStat(null, storeIds,type,status,mobile), HttpStatus.OK);

    }

    /**
     * 删除用户卡券
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity deleteUserCoupon(HttpServletRequest request, @PathVariable("id") Integer id) {

        userCouponService.updateStatus(id, StatusEnum.DISABLE.getKey());
        return new ResponseEntity(HttpStatus.OK);
        // return getSuccessResult(true);
    }

}
